package com.qf.compusdemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.compusdemo.dao.UserDao;
import com.qf.compusdemo.dao.mainDao;
import com.qf.compusdemo.entity.User;
import com.qf.compusdemo.entity.Wxs;
import com.qf.compusdemo.entity.bullet;
import com.qf.compusdemo.service.WxsService;
import com.qf.compusdemo.service.mainService;
import com.qf.compusdemo.vo.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RequestMapping("/boss")
@Controller
public class mainController {
    @Autowired
    private mainService mservice;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private UserDao userDao;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private mainDao mm;

    @Autowired
    private WxsService wxsService;

    @RequestMapping("/allgg")
    @ResponseBody
    public tableVo<bullet> test0(Ppage page, bullet bb){

        System.out.println(page);
        Page<bullet> pp=new Page<>(page.getPage(),page.getLimit());
        IPage<bullet> ipage=mservice.getAllBullet(pp,bb);
        tableVo<bullet> table=new tableVo<>();
        table.setCode("0");
        table.setMsg("ok");
        table.setData(ipage.getRecords());
        table.setCount(ipage.getTotal());
        System.out.println(table);
        return table;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public MessageVo test4(HttpSession session, FormD form) throws IOException {
        System.out.println(form);
        String filename=form.getSource().getOriginalFilename();
        String unique= UUID.randomUUID().toString();
        String ext = FilenameUtils.getExtension(filename);
        String uniqueFileName=unique+"."+ext;
        System.out.println("唯一的文件名："+uniqueFileName);

        String real_path = session.getServletContext().getRealPath("/upload_file");

        File file=new File(real_path);
        if(!file.exists()){
            file.mkdirs();
        }
        String ggmessage=form.getText();
        String username=(String)session.getAttribute("username");
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        User uu=userDao.selectOne(wrapper);

        int mid=uu.getId();
        Date time=new Date();
        bullet bu=new bullet(ggmessage,"/upload_file"+"/"+uniqueFileName,mid,username,time);

        int a=mm.insert(bu);
        MessageVo message=new MessageVo();
        message.setStatus(a>0?true:false);
        message.setMessage(a==1?"success":"fail");
        message.setData(bu);
        form.getSource().transferTo(new File(real_path+"\\"+uniqueFileName));
        return message;
    }

    @RequestMapping("/del")
    @ResponseBody
    public MessageVo test5(Integer[] ids){
        List lists=java.util.Arrays.asList(ids);
        int a=mservice.del(lists);

        MessageVo message=new MessageVo();
        message.setStatus(a>0?true:false);
        message.setMessage(a>0?"success":"fail");
        return message;

    }


    @RequestMapping("/update")
    @ResponseBody
    public MessageVo test6(bullet bb){
        System.out.println(bb);
        int a=mservice.updateBullet(bb);
        MessageVo message=new MessageVo();
        message.setStatus(a>0?true:false);
        message.setMessage(a>0?"success":"fail");
        return message;
    }

    @RequestMapping("/allwx")
    @ResponseBody
    public tableVo<Wxs> test7(Wxs bb, Ppage page){

        Page<Wxs> pp=new Page<>(page.getPage(),page.getLimit());
        IPage<Wxs> ipage=wxsService.getAllwx(pp,bb);
        tableVo<Wxs> table=new tableVo<>();
        table.setCode("0");
        table.setMsg("ok");
        table.setData(ipage.getRecords());
        table.setCount(ipage.getTotal());
        System.out.println(table);
        return table;
    }

    @RequestMapping("/delwx")
    @ResponseBody
    public MessageVo test8(Integer[] ids){
        List lists=java.util.Arrays.asList(ids);
        int a=wxsService.delwx(lists);

        MessageVo message=new MessageVo();
        message.setStatus(a>0?true:false);
        message.setMessage(a>0?"success":"fail");
        return message;

    }



}
